/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import java.net.URI;
import net.scalan.jpa.model.DBSchemObject;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;

/**
 *
 * @author scalan
 */

@XmlRootElement(name = "dBSchemObject")
public class DBSchemObjectConverter {

    private DBSchemObject entity;

    private URI uri;

    private int expandLevel;
  
    /** Creates a new instance of DBSchemObjectConverter */
    public DBSchemObjectConverter() {
        entity = new DBSchemObject();
    }

    /**
     * Creates a new instance of DBSchemObjectConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public DBSchemObjectConverter(DBSchemObject entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getId() + "/").build() : uri;
        this.expandLevel = expandLevel;
    }

    /**
     * Creates a new instance of DBSchemObjectConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public DBSchemObjectConverter(DBSchemObject entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for id.
     *
     * @return value for id
     */
    @XmlElement
    public Long getId() {
        return (expandLevel > 0) ? entity.getId() : null;
    }

    /**
     * Setter for id.
     *
     * @param value the value to set
     */
    public void setId(Long value) {
        entity.setId(value);
    }

    /**
     * Getter for name.
     *
     * @return value for name
     */
    @XmlElement
    public String getName() {
        return (expandLevel > 0) ? entity.getName() : null;
    }

    /**
     * Setter for name.
     *
     * @param value the value to set
     */
    public void setName(String value) {
        entity.setName(value);
    }

    /**
     * Getter for x.
     *
     * @return value for x
     */
    @XmlElement
    public Integer getX() {
        return (expandLevel > 0) ? entity.getX() : null;
    }

    /**
     * Setter for x.
     *
     * @param value the value to set
     */
    public void setX(Integer value) {
        entity.setX(value);
    }

    /**
     * Getter for y.
     *
     * @return value for y
     */
    @XmlElement
    public Integer getY() {
        return (expandLevel > 0) ? entity.getY() : null;
    }

    /**
     * Setter for y.
     *
     * @param value the value to set
     */
    public void setY(Integer value) {
        entity.setY(value);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the DBSchemObject entity.
     *
     * @return an entity
     */
    @XmlTransient
    public DBSchemObject getEntity() {
        if (entity.getId() == null) {
            DBSchemObjectConverter converter = UriResolver.getInstance().resolve(DBSchemObjectConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved DBSchemObject entity.
     *
     * @return an resolved entity
     */
    public DBSchemObject resolveEntity(EntityManager em) {
        return entity;
    }
}
